package com.liancheng.smarthome.module;

import android.databinding.DataBindingUtil;
import android.databinding.ViewDataBinding;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.PopupWindow;

import com.httpapi.QHttpApi;
import com.liancheng.smarthome.R;
import com.liancheng.smarthome.base.BaseVM;
import com.liancheng.smarthome.bean.LoginBean;
import com.liancheng.smarthome.bean.window.InputNumBean;
import com.liancheng.smarthome.databinding.TestDialog;
import com.liancheng.smarthome.manager.HttpManager;
import com.liancheng.smarthome.utils.common.ToastUtil;
import com.liancheng.smarthome.utils.logs.LogTag;
import com.liancheng.smarthome.utils.ui.ForwardPopWindow;

import java.util.HashMap;
import java.util.Map;

/**
 * vm，用来处理逻辑和帮助获取网络，数据库数据的地方
 */
public class ForListVM extends BaseVM<ForListActivity> {

    private ForlistListener netListener;

    /**
     * 获取网络请求对象
     * @return
     */
    public ForlistListener getNetListener() {
        if(this.netListener == null){
            this.netListener = new ForlistListener();
        }
        return netListener;
    }

    public class ForlistListener{
        //登陆方法
        // key-value get 获取后台数据
        public void doLogin(String userName, String pwd ) {
            Map<String, Object> params = new HashMap<>();
            params.put("mobilePhone",userName);
            params.put("appPassword",pwd);
            // 第三个参数，需要用到哪个类解析数据结果就传哪个类进去就行，这里采用了泛型解析
            String url = HttpManager.getIpPort()+"/employeemgn/m/saas/employee-info/login-app";
            ForListVM.this.subscriber=
                    QHttpApi.doJsonPost(url, params, LoginBean.class, HttpManager.loginTag, ForListVM.this);
        }

        //  databinding的弹窗绑定示例
        public void showTestWindow(){
            LogTag.d("启动弹窗");
            View inflate = LayoutInflater.from(context).inflate(R.layout.dialog_input_but, null);
            TestDialog bind = DataBindingUtil.bind(inflate);
//        DisplayMetrics metrics = new DisplayMetrics();
//        context.getWindowManager().getDefaultDisplay().getMetrics(metrics);
//        int width = metrics.widthPixels;
//        int height = metrics.heightPixels;

            ForwardPopWindow mPopWindow = new ForwardPopWindow.PopupWindowBuilder(context).setView(inflate)
                    //显示大小
                    .size(
                            ViewGroup.LayoutParams.MATCH_PARENT,
                            ViewGroup.LayoutParams.WRAP_CONTENT)
                    //弹出popWindow时，背景是否变暗 这个属性设置后 ，会导致界面出现闪烁，不建议设置
                    .setTouchable(true)
                    .setOutsideTouchable(false)
                    .create();
            mPopWindow.showAsDropDown( context.contentView.butLogin, 0, 1, Gravity.CENTER);
            InputNumBean inputNumBean = new InputNumBean();
            inputNumBean.number.set("第一个");
            bind.setInputBean(inputNumBean);
        }
    }

    @Override
    public void onSuccess(int what, Object data) {
        LogTag.d("data-->"+data);
        switch (what){
            case HttpManager.loginTag:
                ToastUtil.showShort("登陆成功");
                break;
                default:
                    LogTag.d("未注册方法");
                    break;
        }
    }

    @Override
    public void onFailure(int what, String msg, int code) {
        showFailMsg(msg);
//        switch (what){
//            case HttpManager.loginTag:
//
//                break;
//            default:
//                LogTag.d("未注册方法");
//                break;
//        }
    }

}
